package edu.office.applicationRepository;


import edu.office.entities.Major;
import edu.office.entities.Majorsubject;
import edu.office.entities.Subject;
import edu.office.tools.StaticFields;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;


@Named("majorsubjectRepositoryA")
@ApplicationScoped
public class MajorsubjectRepository extends ApplicationCommonRepository<Majorsubject> {

    @Inject
    MajorRepository majorRepository;

    public void executUpdate(String sqlString, Major major, Subject subject, String type) {
        switch (type) {
            case StaticFields.OPERATIONDELETE:
                List<Majorsubject> ms = find("select * from Majorsubject  where majorid=" + major.getID()
                        + " and subjectId=" + subject.getID()).list();

                //deal with other objects
//                Set<Major> majorsSet = applicationMajorRepository.getAllSet();
//                Major currentMajor = null;
//                for (Major majorIterator : majorsSet) {
//                    if (majorIterator.equals(major)) {
//                        currentMajor = majorIterator;
//                        break;
//                    }
//                }
//                
                //need to be continued......
                break;
            case StaticFields.OPERATIONINSERT:
                break;
            case StaticFields.OPERATIONUPDATE:
                break;
        }
        update(sqlString);
    }
    public void completeSubjectSelection(Major major, Set<Subject> existedSubjectsSet_A, Set<Subject> nowSelectedSubjects_B) {
        //A--专业已经有的课程，B--新选择的课程
        //get those to be removed:
        Set<Subject> subjectTobeRemoved = new HashSet<>();
        subjectTobeRemoved.addAll(existedSubjectsSet_A);
        subjectTobeRemoved.removeAll(nowSelectedSubjects_B);
        for (Subject subject : subjectTobeRemoved) {
            delete("delete from majorsubject where majorid=" + major.getID()
                    + " and subjectId=" + subject.getID());
        }
        //get those to be added:
        nowSelectedSubjects_B.removeAll(existedSubjectsSet_A);
        for (Subject subject : nowSelectedSubjects_B) {
            Majorsubject majorSubject = new Majorsubject();
            majorSubject.setSUBJECTID(subject.getID());
            majorSubject.setMAJORID(major.getID());
            create(majorSubject);
        }
    }

    public void completeMajorSelection(Subject subject, Set<Major> selectedMajorSet_B) {
        //old MajorSet
        List<Majorsubject> majorsubject4Subject = getMajorsubjects4Subject(subject.getID());
        List<Major> oldMajorlocal = new LinkedList<>();
        majorsubject4Subject.forEach(ms -> {
            oldMajorlocal.add(majorRepository.find("ID", ms.getMAJORID()).firstResult());
        });
        Set<Major> existedMajorSet_A;
        if (!oldMajorlocal.isEmpty()) {
            existedMajorSet_A = new HashSet<>(oldMajorlocal);
        } else {
            existedMajorSet_A = new HashSet<>();
        }
        //new MajorSet
        Set<Major> majorTobeRemoved = new HashSet<>();
        majorTobeRemoved.addAll(existedMajorSet_A);
        majorTobeRemoved.removeAll(selectedMajorSet_B);
        //remove deselected
        for (Major major : majorTobeRemoved) {
            executUpdate("delete from majorsubject where majorid=" + major.getID()
                    + " and subjectId=" + subject.getID(), major, subject, StaticFields.OPERATIONDELETE);
        }
        //Add new selected
        selectedMajorSet_B.removeAll(existedMajorSet_A);
        for (Major major : selectedMajorSet_B) {
            Majorsubject majorSubject = new Majorsubject();
            majorSubject.setSUBJECTID(subject.getID());
            majorSubject.setMAJORID(major.getID());
            create(majorSubject);}
    }
    public List<Majorsubject> getMajorsubjects4Major(Major majorId) {

        return find("majorid", majorId.getID()).list();
    }

    public List<Majorsubject> getMajorsubjects4Subject(Integer subjectId) {
        return find("subjectid", subjectId).list();
    }

    public Set<Major> getMajors4Subject(Integer subject) {
        Set<Major> result = new HashSet<>();
        List<Majorsubject> subjectMajors = getMajorsubjects4Subject(subject);
        subjectMajors.forEach(majorSubject -> result.add(majorRepository.find("ID",majorSubject.getMAJORID()).firstResult()));
        return result;
    }

    public String MajorNames4Subject(Integer subject) {
        String result = "";
        Set<Major> majors = getMajors4Subject(subject);
        result = majors.stream().map((major) -> major.getNAME() + " ").reduce(result, String::concat);
        return result;
    }
}
